; ORIGINAL: 3dt2.1/examples/splnsurf.scm
; $Id: splnsurf.scm,v 1.5 1998/12/09 23:01:21 jmb Exp $
; -------------------------------------------------------------------------
;   File: SPLNSURF.SCM
;  Notes: Scheme procedures to take a splsurf data type and intialize its
;         values to data read from a disk file.  The file format is:
;
;         u_degree u_rational u_form u_pole u_num_ctrlpts
;         v_degree v_rational v_form v_pole v_num_ctrlpts
;         list of u_knots
;         list of v_knots
;         list of contol points: u (rows) x v (cols)
;             - each item ( %-8.4f ):  x  y  z
;         list of weights: u (rows) x v (cols)
;             - each item ( %-8.4f ):  wt
; -------------------------------------------------------------------------

; set the view
;
(view:dl 0 0 500 400)
(view:set (position 100 -200 100) (position 0 0 0) (gvector 0 0 1))

; set system options
;
(option:set "spline_param_lines" #t)
(option:set "u_param_lines" 4)
(option:set "v_param_lines" 4)

; surface file read procedure
;
(define splsurf:read (lambda (surf filename)
    (begin
        ; open data file
        ;(define dataport (open-input-file-path filename part-load-path))
	(define dataport (open-input-file filename))

        ; u parameter
        (define u_degree   (read dataport))
        (define u_rational (read dataport))
        (define u_form     (read dataport))
        (define u_pole     (read dataport))
        (define u_ctrlpts  (read dataport))

        (splsurf:set-u-param surf u_degree u_rational u_form u_pole)

        ; v parameter
        (define v_degree   (read dataport))
        (define v_rational (read dataport))
        (define v_form     (read dataport))
        (define v_pole     (read dataport))
        (define v_ctrlpts  (read dataport))

        (splsurf:set-v-param surf v_degree v_rational v_form v_pole)

        ; u knots
        (define u_knot_count (+ u_ctrlpts u_degree 1))
        (define knot           0)
        (define knot_list (list))

        (if (not (zero? u_knot_count))
            (do ((count 0 (+ count 1))) ((= count u_knot_count))
                (begin
                    (set! knot (read dataport))
                    (set! knot_list (append knot_list (list knot)))
                )
            )
        )
        (splsurf:set-u-knot-list surf knot_list u_knot_count)

        ; v knots
        (define v_knot_count (+ v_ctrlpts v_degree 1))
        (set! knot           0)
        (set! knot_list (list))

        (if (not (zero? v_knot_count))
            (do ((count 0 (+ count 1))) ((= count v_knot_count))
                (begin
                    (set! knot (read dataport))
                    (set! knot_list (append knot_list (list knot)))
                )
            )
        )
        (splsurf:set-v-knot-list surf knot_list v_knot_count)

        ; read control point and weight values
        (define x            0)
        (define y            0)
        (define z            0)
        (define ctrlpts (list))

        (do ((count 0 (+ count 1))) ((= count (* u_ctrlpts v_ctrlpts)))
            (begin
                (set! x (read dataport))
                (set! y (read dataport))
                (set! z (read dataport))
                (set! ctrlpts (append ctrlpts (list (position x y z))))
            )
        )
        (splsurf:set-ctrlpt-list surf ctrlpts u_ctrlpts v_ctrlpts)

        ; read weight values
        (define w            0)
        (define weights (list))

        (if (or (> u_rational 0) (> v_rational 0))
            (begin
                (do ((count 0 (+ count 1))) ((= count (* u_ctrlpts v_ctrlpts)))
                    (begin
                        (set! w (read dataport))
                        (set! weights (append weights (list w)))
                    )
                )
                (splsurf:set-weight-list surf weights)
            )
        )

        ; close data file
        (close-input-port dataport)
    )
))

; create a spline surface
;
(define surf (splsurf))
(splsurf:read surf "surf01.dat")

; display a spline surface face from the surface data
;
(face:spline-ctrlpts surf)

; ---------------------------------
;  End of SPLNSURF.SCM
; ---------------------------------
